package com.itbooking.core.response;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Slf4j
public class GlobalExeceptionHandler {


    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Throwable.class)
    public ErrorResult handlerExcetion(Throwable e, HttpServletRequest request){
        ErrorResult errorResult = ErrorResult.fail(ResponseCode.SERVER_ERROR,e);
        log.error(" 出现了 BusinessException异常, 地址是：{}，异常是：{}",request.getRequestURI(),errorResult);
        return  errorResult;
    }


    @ExceptionHandler(BusinessException.class)
    public ErrorResult handlerBusinessException(BusinessException e, HttpServletRequest request){
        ErrorResult errorResult = ErrorResult.builder().status(e.getCode())
                .message(e.getMessage())
                .exception(e.getClass().getName())
                .build();
        log.warn(" 出现了 BusinessException异常, 地址是：{}，异常是：{}",request.getRequestURI(),errorResult);
        return  errorResult;
    }

}
